package com.zkingsoft.services.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.ExtensionDetailDao;
import com.zkingsoft.dao.RechargeDetailDao;
import com.zkingsoft.dao.TradeRecordDao;
import com.zkingsoft.dao.UserdataDao;
import com.zkingsoft.dao.UsersDao;
import com.zkingsoft.model.CashApply;
import com.zkingsoft.model.Clubdata;
import com.zkingsoft.model.ExtensionDetail;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.RechargeDetail;
import com.zkingsoft.model.TradeRecord;
import com.zkingsoft.model.Userdata;
import com.zkingsoft.model.Users;
import com.zkingsoft.services.CashApplyService;
import com.zkingsoft.services.ClubdataService;
import com.zkingsoft.services.TradeRecordService;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-07-17 13:45
 */
@Service("tradeRecordService")
public class  TradeRecordServiceImpl implements    TradeRecordService  {

	
	@Autowired
	private TradeRecordDao tradeRecordDao;
	@Autowired
	private UsersDao usersDao;
	@Autowired
	private UserdataDao userDataDao;
	@Autowired
	private RechargeDetailDao rechargeDao;
	@Autowired
	private ExtensionDetailDao extensionDao;
	@Autowired
	private CashApplyService cashService;
	@Autowired
	private ClubdataService clubService;
	
	
	@Override
	public int add(TradeRecord tradeRecord){
		
		return tradeRecordDao.insert(tradeRecord);
		
	}
   	
    @Override
	public int modify(TradeRecord tradeRecord){
	
		return tradeRecordDao.update(tradeRecord);
	
	}
	
	@Override
	public int remove(List<Long> list){
	
		return tradeRecordDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long id){
	
		return tradeRecordDao.deleteById(id);
	
	}
	
	@Override
	public List<TradeRecord> findInPage(TradeRecord tradeRecord,  PaginationVO pageVo){
	
		return tradeRecordDao.selectInPage(tradeRecord , pageVo);
	
	}
	
	@Override
	public List<TradeRecord> findByModel(TradeRecord tradeRecord){
	
		return tradeRecordDao.selectByModel(tradeRecord);
	
	}
	
	@Override
	public int  findTotal(TradeRecord tradeRecord){
	
		return tradeRecordDao.selectTotalRecord(tradeRecord);
	
	}
	
	@Override
	public TradeRecord  findById(Long id){
	
		return tradeRecordDao.selectById(id);
	
	}

	@Override
	public TradeRecord findByCode(String code) {
		// TODO Auto-generated method stub
		return tradeRecordDao.selectByCode(code);
	}

	@Override
	@Transactional
	public boolean addTradeRecord(TradeRecord tradeRecord,int status,String trade_no,String clubName) {
		if(tradeRecord!=null&&tradeRecord.getStatus()!=1){//待付款
			tradeRecord.setStatus(status);//设为交易成功
			tradeRecord.setAlipayTradeCode(trade_no);
			int num = tradeRecordDao.insert(tradeRecord);
			if(num>0){//添加交易记录成功
				return true;
			}
		}
		return false;
	}

	@Override
	@Transactional
	public void dealRechargeBusindess(TradeRecord tradeRecord,int status,String trade_no,String clubName) {
		if(addTradeRecord(tradeRecord, status,trade_no,clubName)){
			RechargeDetail rechargeDetail = rechargeDao.selectById(tradeRecord.getRechargeId());
			//查出当前用户
			Userdata userData = userDataDao.selectById(Integer.parseInt(tradeRecord.getUserId().toString()));
			//用户当前钻石数量
			int userDiamondNum = userData.getDiamondnum()==null?0:userData.getDiamondnum();
			if(rechargeDetail.getType()==1){//充值俱乐部
				if(userData.getCreateclubid()==-1){//从未购买俱乐部
					Clubdata club = new Clubdata();
					club.setClubname(clubName);
					club.setClubmasterid(userData.getUserid().toString());
					club.setCreatetime((int)(new Date().getTime()/1000));
					//添加俱乐部名称
					club.setClubmastername(userData.getName());
					club.setClubmembernum(0);
					club.setClubroomnum(0);
					int num = clubService.add(club);
					if(num>0){//更新userdata表
						Clubdata clubData =	clubService.findByUserId(userData.getUserid().toString());
						userData.setCreateclubid(clubData.getClubid());
						//加入自己创建的俱乐部 by mlg
						for(int i=0; i<10; i++) {
							Integer joinclubid = 0;
							switch(i) {
								case 0 : joinclubid = userData.getJoinclubid0();break;
								case 1 : joinclubid = userData.getJoinclubid1();break;
								case 2 : joinclubid = userData.getJoinclubid2();break;
								case 3 : joinclubid = userData.getJoinclubid3();break;
								case 4 : joinclubid = userData.getJoinclubid4();break;
								case 5 : joinclubid = userData.getJoinclubid5();break;
								case 6 : joinclubid = userData.getJoinclubid6();break;
								case 7 : joinclubid = userData.getJoinclubid7();break;
								case 8 : joinclubid = userData.getJoinclubid8();break;
								case 9 : joinclubid = userData.getJoinclubid9();break;
							}
							if(joinclubid != null && joinclubid != 0) {
								continue;
							} else {
								switch(i) {
									case 0 : userData.setJoinclubid0(joinclubid);break;
									case 1 : userData.setJoinclubid1(joinclubid);break;
									case 2 : userData.setJoinclubid2(joinclubid);break;
									case 3 : userData.setJoinclubid3(joinclubid);break;
									case 4 : userData.setJoinclubid4(joinclubid);break;
									case 5 : userData.setJoinclubid5(joinclubid);break;
									case 6 : userData.setJoinclubid6(joinclubid);break;
									case 7 : userData.setJoinclubid7(joinclubid);break;
									case 8 : userData.setJoinclubid8(joinclubid);break;
									case 9 : userData.setJoinclubid9(joinclubid);break;
								}
								break;
							}
						}
						
						//按俱乐部赠送数量赠送钻石
						userData.setDiamondnum((rechargeDetail.getGiveNum()==null?0:rechargeDetail.getGiveNum())+userDiamondNum);
					}
				}else{//购买过俱乐部，直接按赠送数量充值钻石
					userData.setDiamondnum((rechargeDetail.getGiveNum()==null?0:rechargeDetail.getGiveNum())+userDiamondNum);
				}
			}else if(rechargeDetail.getType()==2){//充值钻石
				//充值钻石数=钻石数量+赠送数量
				int rechargeTotalNum = rechargeDetail.getNum()+(rechargeDetail.getGiveNum()==null?0:rechargeDetail.getGiveNum());
				userData.setDiamondnum(userDiamondNum+rechargeTotalNum);
			}
			userDataDao.update(userData);
			//如有推广人，需返利
			ExtensionDetail extension = extensionDao.selectByWeiXinId(userData.getWxid());
			if(extension!=null){//下载时绑定了推广人
					Users userParent = usersDao.selectById(extension.getUserParentId());
					Double extensionMoney = (userParent.getExtensionMoney()==null?0:userParent.getExtensionMoney())+(rechargeDetail.getBackMoney()==null?0:rechargeDetail.getBackMoney());
					userParent.setExtensionMoney(extensionMoney);
					//修改用户的推广金
					int num = usersDao.update(userParent);
					if(num>0){//添加一天推广佣金记录
						ExtensionDetail extensionDetail = new ExtensionDetail();
						extensionDetail.setUserId(tradeRecord.getUserId());
						extensionDetail.setUserParentId(userParent.getId());
						extensionDetail.setDescriptions("充值"+rechargeDetail.getMoney()+"元");
						extensionDetail.setMoney(rechargeDetail.getBackMoney()==null?0:rechargeDetail.getBackMoney());
						extensionDetail.setCreateTime(new Date().getTime());
						extensionDetail.setStatus(2);
						extensionDao.insert(extensionDetail);
				}
			}
		}
	}

	//计算会员时间
//	public Integer getClubTime(int level){
//		Integer nowDate = (int) (new Date().getTime()/1000);//当前时间(s)
//		Integer monthTimes = 2592000;//一个月按30天，1天=86400秒(s)
//		return nowDate+monthTimes*level;
//	}

		
		/**
		 * 提现
		 */
		@Override
		@Transactional
		public boolean postCash(Integer gameId,String account) {
			Users user = usersDao.selectByGameId(gameId);
			if(user.getExtensionMoney()!=null&&user.getExtensionMoney()>0){
				//添加一条提现记录
				CashApply cash = new CashApply();
				cash.setUserId(user.getId());
				cash.setUserName(user.getName());
				cash.setUserTel(account);
				cash.setMoney(user.getExtensionMoney());
				cash.setStatus(2);//未处理
				cash.setCreateTime(new Date().getTime());
				int result = cashService.add(cash);
				if(result>0){//同时扣除用户推广金
					user.setExtensionMoney(0.0);
					int num = usersDao.cleanExtensionMoney(user.getId());
					if(num>0){
						return true;
					}
				}
			}
			return false;
		}

		@Override
		public int findRequestTimesByGameId(Long gameId,Long startTime, Long nowTime) {
			// TODO Auto-generated method stub
			return tradeRecordDao.selectRequestTimesByGameId(gameId,startTime,nowTime);
		}
	
	
}